package com.basic.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

class ImageUtil {

    /**
     * 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     *
     * @author linbingwen
     * @since 2016年3月30日
     * @param imgFilePath 图片路径
     * @return base64编码的字符串
     * @throws IOException
     */
    public static String imageToBase64(String imgFilePath) throws IOException {
        byte[] data = null;
        InputStream in = null;
        try {
            in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];	// 读取图片字节数组
            in.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
        }
        BASE64Encoder encoder = new BASE64Encoder();// 对字节数组Base64编码
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    /**
     * 对字节数组字符串进行Base64解码并生成图片
     *
     * @author linbingwen
     * @since 2016年3月30日
     * @param base64 base64编码的数据
     * @param imgFilePath 要保存的图片路径
     * @param imgFileName 要保存的图片名
     * @return
     * @throws IOException
     */
    public static Boolean base64ToImage(String base64, String imgFilePath,String imgFileName) throws IOException {
        if (base64 == null) {
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            byte[] bytes = decoder.decodeBuffer(base64);// Base64解码
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            InputStream input = new ByteArrayInputStream(bytes);
            out = new FileOutputStream(imgFilePath + imgFileName);// 生成jpeg图片
            out.write(bytes);
            out.flush();
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 将base64编码的字符串转成InputStream
     * @author linbingwen
     * @since  2016年3月30日
     * @param base64
     * @return InputStream
     */
    public static InputStream base64ToInputStream(String base64) {
        if (base64 == null) {
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] bytes = decoder.decodeBuffer(base64);// Base64解码
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            InputStream input = new ByteArrayInputStream(bytes);
            return input;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将网络图片进行Base64位编码
     * @author linbingwen
     * @since  2016年3月30日
     * @param imageUrl 图片的url路径，如http://.....xx.jpg
     * @return 返回Base64编码过的字节数组字符串
     * @throws IOException
     */
    public static String imageUrlToBase64(String imageUrl) throws IOException {
        ByteArrayOutputStream outputStream = null;
        try {
            URL  url = new URL(imageUrl);
            BufferedImage bufferedImage = ImageIO.read(url);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
        BASE64Encoder encoder = new BASE64Encoder();// 对字节数组Base64编码
        return encoder.encode(outputStream.toByteArray());
    }

    /**
     *
     * @author linbingwen
     * @since  2016年4月13日
     * @param imageUrl 图片url
     * @param imageType 图片格式 如 jpg/bmp/png
     * @return
     * @throws IOException
     */
    public static byte[] imageUrlToBytes(String imageUrl,String imageType) throws IOException {
        ByteArrayOutputStream outputStream = null;
        byte[] bytes  = null;
        try {
            URL  url = new URL(imageUrl);
            BufferedImage bufferedImage = ImageIO.read(url);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, imageType, outputStream);
            bytes = outputStream.toByteArray();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
        return bytes;
    }

    public static void main(String[] args) throws IOException {
        String imgFilePath = "D:\\";
        String  ttString = imageUrlToBase64("http://10.75.201.68:8888/cfile/file/image?imageId=group2/M00/05/25/CkvJolb5nIOAcHMZAADU5zn6AlI7014866");
        base64ToImage(ttString, imgFilePath, "44.bmp");

        urlBase64ToFile("http://10.75.201.68:8888/cfile/file/image?imageId=group2/M00/05/97/CkvJo1dxbqKAEBqzAARHjfpHsPk7518600");
    }

    /**
     * @author linbingwen
     * @since  2016年6月28日
     * @param string
     */
    private static void urlBase64ToFile(String string) {

    }


}
